package com.sean.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;



public class FileUtil {
	public static void deleteFile(File file){
		Log.i("FileUtil", " FileUtil  >> deleteFile >>  " + file.getPath());
		if(file.exists()){
			if(file.isFile()){
				file.delete();
			}else{
				File[] files = file.listFiles();
				if(files!=null){
					for(File f : files){
						deleteFile(f);
					}
				}
				file.delete();
			}
		}
	}
	
	public static void deleteFile(String filePath){
		if(filePath!=null){
			File file = new File(filePath);
			deleteFile(file);
		}
	}
	
	public static void copyFile(File from, File to){
		if(!from.exists()){
			return;
		}
		 FileInputStream in = null;
		 FileOutputStream out = null;
		 try{
		  	 in = new FileInputStream(from);
		  	 out = new FileOutputStream(to);
		  	 
		  	 byte bt[] = new byte[1024];
			    int c;
			    while ( (c = in.read(bt)) > 0) {
			    	out.write(bt, 0, c);
			    }
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
				try {
					 if(in!=null){
						 in.close();
					 }
					 if(out!=null){
						 out.close();
					 }
		    } catch ( IOException e ) {
		        e.printStackTrace();
		    }
			 
		 }
   }
	
	public static void writeFileEnd(String filepath,String text){
		try{
			File file = new File(filepath);
			if(!file.exists()){
				file.getParentFile().mkdirs();
			}
			OutputStreamWriter wr = new OutputStreamWriter(new FileOutputStream(filepath,true),"UTF8");
			wr.write(text);
			wr.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static String createCacheFile(String fileName, Context context){
		File cacheDir = context.getCacheDir();
		String path = cacheDir.getPath() + "/" + fileName;
		File file = new File(path);
		if(!file.exists()){
			file.getParentFile().mkdir();
		}
		return path;
	}
	
	public static String readFile(String path){
		StringBuffer str = new StringBuffer("");
		try{
			InputStreamReader ir = new InputStreamReader(new FileInputStream(path),"UTF8");
			BufferedReader br = new BufferedReader(ir);
			String data = br.readLine();
			
			while(data!=null){
				str.append(data);
				data = br.readLine();
			}
			br.close();
			ir.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return str.toString();
	}
 
	public static List<String> readFileOfList(String path){
		List<String> list = new ArrayList<String>();
		try{
			InputStreamReader ir = new InputStreamReader(new FileInputStream(path),"UTF8");
			BufferedReader br = new BufferedReader(ir);
			String data = br.readLine();
			
			while(data!=null){
				list.add(data);
				data = br.readLine();
			}
			br.close();
			ir.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
